package com.ygxsk.carhome.ui.care;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ColorUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.lxj.xpopup.core.BottomPopupView;
import com.lxj.xpopup.util.XPopupUtils;
import com.ygxsk.carhome.R;
import com.ygxsk.carhome.constant.ConstantSign;
import com.ygxsk.carhome.constant.ExtraValue;
import com.ygxsk.carhome.listener.RxBus;
import com.ygxsk.carhome.response.AgreenMentFileAddSubmit;
import com.ygxsk.carhome.response.EvaluateWadeInfo;
import com.ygxsk.carhome.rxbus.RxBusCarWade;
import com.ygxsk.carhome.rxbus.RxBusCarWadeForAgreement;
import com.ygxsk.carhome.utils.CommUtils;
import com.ygxsk.carhome.views.MyLinearLayoutManager;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/*
 * @Author:itchenqi175@163.com
 * @Time:2023-08-09  16:18
 * @Description:将这个布局设置为通用的
 */
public class CardTwoListPopu extends BottomPopupView {

    public CardTwoListPopu(@NonNull Context context) {
        super(context);
    }

    //因为通用界面，标志从哪个页面过来；
    private int extra_jumpfrom_where;

    private ArrayList<EvaluateWadeInfo> evaluate_wade_list_outSide = new ArrayList<>();

    //对于客户的跟进状态的适配器
    private CardTwoListInfoAdapter cardTwoListInfoAdapter;

    //这条合同的数据，由点击类型携带过来，然后再次由传递出去，携带给界面，填充数据
    private AgreenMentFileAddSubmit agreenMentFileAddSubmit;


    //弹窗的标题
    private String titleName;

    public void setTitleName(String _titleName) {
        titleName = _titleName;
    }

    public void setAgreenMentFileAddSubmit(AgreenMentFileAddSubmit _agreenMentFileAddSubmit) {
        this.agreenMentFileAddSubmit = _agreenMentFileAddSubmit;
    }

    public CardTwoListPopu(@NonNull Context context, int _extra_action, ArrayList<EvaluateWadeInfo> _stationDatas) {
        super(context);

        //不这样等于，直接 赋值 添加；
        //station_list_outSide = _stationDatas;
        extra_jumpfrom_where = _extra_action;

        if (_stationDatas != null && !_stationDatas.isEmpty()) {

            if (evaluate_wade_list_outSide != null && !evaluate_wade_list_outSide.isEmpty()) {
                evaluate_wade_list_outSide.clear();
            }

            if (evaluate_wade_list_outSide != null) {
                evaluate_wade_list_outSide.addAll(_stationDatas);
            }

        }

    }


    @Override
    protected int getImplLayoutId() {
        return R.layout.populeadsfromwhere;
    }


    @Override
    protected void onCreate() {
        super.onCreate();

//        LogUtils.d(ConstantSign.LOGCAT_NOMAL + "onCreate--onCreate---onCreate--onCreate--onCreate");

        //搜索布局我没有添加

        //取消按钮
        TextView btn_dialog_search_cancel = (TextView) this.findViewById(R.id.btn_dialog_search_cancel);
        //确定按钮
        TextView btn_dialog_search_decide = (TextView) this.findViewById(R.id.btn_dialog_search_decide);
        //中间的标题内容
        TextView titleNameWhereMiddle = (TextView) this.findViewById(R.id.titlenamewheremiddle);

        if (titleNameWhereMiddle != null) {
            titleNameWhereMiddle.setText(titleName);
        }

        //初始化适配器列表
        RecyclerView recyclerViewInStation = (RecyclerView) this.findViewById(R.id.recyclerviewinstationasset);
        MyLinearLayoutManager myLinearLayoutManager = new MyLinearLayoutManager(this.getContext(), LinearLayoutManager.VERTICAL, false);
        recyclerViewInStation.setLayoutManager(myLinearLayoutManager);

        //适配器的创建；页面适配器使用的是一个条目项布局
        cardTwoListInfoAdapter = new CardTwoListInfoAdapter(R.layout.popustationinfoadaptercardtwo, evaluate_wade_list_outSide);
        recyclerViewInStation.setAdapter(cardTwoListInfoAdapter);

        if (cardTwoListInfoAdapter != null) {
            cardTwoListInfoAdapter.notifyDataSetChanged();
        }

        //单独的点击条目
        cardTwoListInfoAdapter.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {

                //直接设置勾选；
                EvaluateWadeInfo evaluateWadeInfo = (EvaluateWadeInfo) adapter.getItem(position);

                if (evaluateWadeInfo != null) {

                    //发送消息，给业务台账，填充选中的业务类型；
                    for (EvaluateWadeInfo itemBottom : cardTwoListInfoAdapter.getData()) {

                        // TODO: 2024/2/1 22:16  这种是不是只能用车牌匹配
                        //是唯一的；
//                        if (TextUtils.equals(evaluateWadeInfo.getDictCode(), itemBottom.getDictCode())) {
                        if (TextUtils.equals(evaluateWadeInfo.getCarNumber(), itemBottom.getCarNumber())) {

                            if (itemBottom.isChecked()) {
                                itemBottom.setChecked(false);
                            } else {
                                itemBottom.setChecked(true);
                            }

                            LogUtils.json("itchen--选中的", itemBottom);

                        } else {
                            itemBottom.setChecked(false);
                        }

                    }

                    //是单选，直接刷新；
                    if (cardTwoListInfoAdapter != null) {
                        cardTwoListInfoAdapter.notifyDataSetChanged();
                    }

                }
            }
        });

        //【取消】按钮
        if (btn_dialog_search_cancel != null) {
            btn_dialog_search_cancel.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
        }

        //【确定】按钮
        if (btn_dialog_search_decide != null) {
            btn_dialog_search_decide.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {

                    //method_test();

                    //即将被传递的单个item;
                    EvaluateWadeInfo evaluateWadeInfo_upLoad = null;

                    if (evaluate_wade_list_outSide != null && !evaluate_wade_list_outSide.isEmpty()) {

                        //----------------------------------------------------------------------------------------
                        Iterator<EvaluateWadeInfo> iterator = evaluate_wade_list_outSide.iterator();

                        if (iterator != null) {

                            while (iterator.hasNext()) {

                                EvaluateWadeInfo item = iterator.next();

                                if (item != null) {

                                    if (item.isChecked()) {
                                        evaluateWadeInfo_upLoad = item;
                                        //LogUtils.json(ConstantSign.LOGCAT_NOMAL + "选中条目项是->", item);
                                        break;
                                    }

                                }
                            }
                        }
                        //----------------------------------------------------------------------------------------
                        if (evaluateWadeInfo_upLoad != null) {

//                            if (extra_jumpfrom_where == ExtraValue.MODIFY_EVALUATE_CAR_WADE_SELECT) {
//
//                                //标识是从哪个页面过来的；
//                                RxBusCarWade rxBusCarWade = new RxBusCarWade();
//                                rxBusCarWade.setActionFlag(ExtraValue.MODIFY_EVALUATE_CAR_WADE_SELECT);
//                                rxBusCarWade.setEvaluateWadeInfo(evaluateWadeInfo_upLoad);
//                                RxBus.getInstance().post(rxBusCarWade);
//
//                            } else

                            if (extra_jumpfrom_where == ExtraValue.MODIFY_EVALUATE_CAR_PRICETYPE_SELECT) {

                                //标识是从哪个页面过来的；
                                RxBusCarWade rxBusCarWade = new RxBusCarWade();
                                rxBusCarWade.setActionFlag(ExtraValue.MODIFY_EVALUATE_CAR_PRICETYPE_SELECT);
                                rxBusCarWade.setEvaluateWadeInfo(evaluateWadeInfo_upLoad);
                                RxBus.getInstance().post(rxBusCarWade);

                            } else if (extra_jumpfrom_where == ExtraValue.MODIFY_EVALUATE_CUSTOMER_STATE_SELECT) {
                                //标志是：预约维保 模块，详情-客户的状态
                                RxBusCarWade rxBusCarWade = new RxBusCarWade();
                                rxBusCarWade.setActionFlag(ExtraValue.MODIFY_EVALUATE_CUSTOMER_STATE_SELECT);
                                rxBusCarWade.setEvaluateWadeInfo(evaluateWadeInfo_upLoad);
                                RxBus.getInstance().post(rxBusCarWade);

                            } else if (extra_jumpfrom_where == ExtraValue.MODIFY_EVALUATE_AUTH_SYSTEM_SELECT) {

                                RxBusCarWade rxBusCarWade = new RxBusCarWade();
                                rxBusCarWade.setActionFlag(ExtraValue.MODIFY_EVALUATE_AUTH_SYSTEM_SELECT);
                                rxBusCarWade.setEvaluateWadeInfo(evaluateWadeInfo_upLoad);
                                RxBus.getInstance().post(rxBusCarWade);

                            } else if (extra_jumpfrom_where == ExtraValue.MODIFY_EVALUATE_CARMAIN_PERSON_SELECT) {

                                //客户关怀-卡券发放
                                RxBusCarWade rxBusCarWade = new RxBusCarWade();
                                rxBusCarWade.setActionFlag(ExtraValue.MODIFY_EVALUATE_CARMAIN_PERSON_SELECT);
                                rxBusCarWade.setEvaluateWadeInfo(evaluateWadeInfo_upLoad);
                                RxBus.getInstance().post(rxBusCarWade);

                            } else if (extra_jumpfrom_where == ExtraValue.MODIFY_EVALUATE_RICE_TICKET_PERSON_SELECT) {

                                //是从餐券发放的页面过来选择人员的
                                RxBusCarWade rxBusCarWade = new RxBusCarWade();
                                rxBusCarWade.setActionFlag(ExtraValue.MODIFY_EVALUATE_RICE_TICKET_PERSON_SELECT);
                                rxBusCarWade.setEvaluateWadeInfo(evaluateWadeInfo_upLoad);
                                RxBus.getInstance().post(rxBusCarWade);

                            } else if (extra_jumpfrom_where == ExtraValue.VALUE_JUMP_FORM_CARD_MAIN_CARD_TYPE) {

                                //卡券发放的类型
                                RxBusCarWade rxBusCarWade = new RxBusCarWade();
                                rxBusCarWade.setActionFlag(ExtraValue.VALUE_JUMP_FORM_CARD_MAIN_CARD_TYPE);
                                rxBusCarWade.setEvaluateWadeInfo(evaluateWadeInfo_upLoad);
                                RxBus.getInstance().post(rxBusCarWade);

                            } else if (extra_jumpfrom_where == ExtraValue.VALUE_JUMP_FORM_CARD_MAIN_GREAT_PACKAGE_TYPE) {

                                LogUtils.d(ConstantSign.LOGCAT_NOMAL + "选中的是  礼包的形式");
                                //客户关怀-发放的是 优惠礼包 的形式
                                RxBusCarWade rxBusCarWade = new RxBusCarWade();
                                rxBusCarWade.setActionFlag(ExtraValue.VALUE_JUMP_FORM_CARD_MAIN_GREAT_PACKAGE_TYPE);
                                rxBusCarWade.setEvaluateWadeInfo(evaluateWadeInfo_upLoad);
                                RxBus.getInstance().post(rxBusCarWade);

                            } else if (extra_jumpfrom_where == ExtraValue.MODIFY_SMART_GO_HAS_FEES) {

                                // 智能出门的  是否产生费用  //是否产生费用:0：否；1：是
                                RxBusCarWade rxBusCarWade = new RxBusCarWade();
                                rxBusCarWade.setActionFlag(ExtraValue.MODIFY_SMART_GO_HAS_FEES);
                                rxBusCarWade.setEvaluateWadeInfo(evaluateWadeInfo_upLoad);
                                RxBus.getInstance().post(rxBusCarWade);

                            } else if (extra_jumpfrom_where == ExtraValue.MODIFY_SMART_GO_IS_PAYMENT) {

                                // 智能出门的 是否已缴费(0:否，1:是)
                                RxBusCarWade rxBusCarWade = new RxBusCarWade();
                                rxBusCarWade.setActionFlag(ExtraValue.MODIFY_SMART_GO_IS_PAYMENT);
                                rxBusCarWade.setEvaluateWadeInfo(evaluateWadeInfo_upLoad);
                                RxBus.getInstance().post(rxBusCarWade);

                            } else if (extra_jumpfrom_where == ExtraValue.MODIFY_SMART_GO_BUINESS_TYPE_SELECT) {//

                                // 智能出门的 是否已缴费(0:否，1:是)
                                RxBusCarWade rxBusCarWade = new RxBusCarWade();
                                rxBusCarWade.setActionFlag(ExtraValue.MODIFY_SMART_GO_BUINESS_TYPE_SELECT);
                                rxBusCarWade.setEvaluateWadeInfo(evaluateWadeInfo_upLoad);
                                RxBus.getInstance().post(rxBusCarWade);

                            } else if (extra_jumpfrom_where == ExtraValue.MODIFY_ARCHIVEFILE_AGREEMENT_TYPE_SELECT) {

                                //如果是添加合同的，选择这个合同的类型
                                RxBusCarWadeForAgreement rxBusCarWadeForAgreement = new RxBusCarWadeForAgreement();
                                rxBusCarWadeForAgreement.setActionFlag(ExtraValue.MODIFY_ARCHIVEFILE_AGREEMENT_TYPE_SELECT);
                                rxBusCarWadeForAgreement.setEvaluateWadeInfo(evaluateWadeInfo_upLoad);

                                //给传递的这条数据，再携带回来；
                                rxBusCarWadeForAgreement.setAgreenMentFileAddSubmit(agreenMentFileAddSubmit);

                                RxBus.getInstance().post(rxBusCarWadeForAgreement);

                            } else if (extra_jumpfrom_where == ExtraValue.MODIFY_EVALUATE_SIGNTEMPLATENAME_SELECT) {

                                //如果是 法大大认证的合同 的  添加合同的，选择这个合同的类型
                                RxBusCarWade rxBusCarWade = new RxBusCarWade();
                                rxBusCarWade.setActionFlag(ExtraValue.MODIFY_EVALUATE_SIGNTEMPLATENAME_SELECT);
                                rxBusCarWade.setEvaluateWadeInfo(evaluateWadeInfo_upLoad);
                                RxBus.getInstance().post(rxBusCarWade);

                            }

                            //关闭该弹窗内容
                            dismiss();

                        } else {

                            // TODO: 2023-08-12 09:53 switch做分别的弹窗；
                            //请选择 特别注意：这里只能使用通用弹窗，
                            ToastUtils.showShort(R.string.select_evaluate_value_nomal);

                        }

                    } else {
                        //执行异常？
                        ToastUtils.showShort(StringUtils.getString(R.string.action_abnormal));
                    }

                }
            });
        }

    }

    @Override
    protected void onShow() {
        super.onShow();

        LogUtils.d(ConstantSign.LOGCAT_NOMAL + "界面弹窗 弹出");

    }


    @Override
    public void onDestroy() {
        //        LogUtils.d(ConstantSign.LOGCAT_NOMAL + "弹窗--弹窗--弹窗onDestroy--onDestroy---onDestroy--onDestroy--onDestroy");
        if (evaluate_wade_list_outSide != null && !evaluate_wade_list_outSide.isEmpty()) {

            for (EvaluateWadeInfo evaluateWadeInfo : evaluate_wade_list_outSide) {
                evaluateWadeInfo.setChecked(false);
            }

            if (cardTwoListInfoAdapter != null) {
                cardTwoListInfoAdapter.notifyDataSetChanged();
            }

        }
        super.onDestroy();

    }

    @Override
    protected boolean onBackPressed() {

//        LogUtils.d(ConstantSign.LOGCAT_NOMAL + "拦截 弹出弹窗的  返回事件");

//        CommUtils.checkDialog(alertViewDialogSure);

//        if (mDatasResources!=null&&!mDatasResources.isEmpty()){
//        }

        return super.onBackPressed();
    }

    @Override
    protected int getMaxHeight() {

        //如果是报价类型，那么缩小高度
        int currentHeight = (int) (XPopupUtils.getScreenHeight(getContext()) * .6f);

        if (extra_jumpfrom_where == ExtraValue.MODIFY_EVALUATE_CAR_PRICETYPE_SELECT//车辆评估-报价类型
                || extra_jumpfrom_where == ExtraValue.MODIFY_EVALUATE_CUSTOMER_STATE_SELECT//车辆维保详情-客户状态
                || extra_jumpfrom_where == ExtraValue.MODIFY_EVALUATE_CARMAIN_PERSON_SELECT//发送客户优惠券或者优惠礼包
                || extra_jumpfrom_where == ExtraValue.MODIFY_EVALUATE_RICE_TICKET_PERSON_SELECT//发送客户餐券
                || extra_jumpfrom_where == ExtraValue.MODIFY_ARCHIVEFILE_AGREEMENT_TYPE_SELECT//客户档案-档案添加-合同类型。
        ) {
            currentHeight = (int) (XPopupUtils.getScreenHeight(getContext()) * .4f);
        } else if (extra_jumpfrom_where == ExtraValue.MODIFY_SMART_GO_BUINESS_TYPE_SELECT) {
            //出门证的业务类型只需要高度一半
            currentHeight = (int) (XPopupUtils.getScreenHeight(getContext()) * .5f);
        }

        return currentHeight;
    }

    /*
     * @Author:itchenqi175@163.com
     * @Time:2023-07-12  15:22
     * @Description:选择列表布局的适配器
     */
    //适配器内容：
    private class CardTwoListInfoAdapter extends BaseQuickAdapter<EvaluateWadeInfo, BaseViewHolder> {

        public CardTwoListInfoAdapter(int layoutResId, @Nullable List<EvaluateWadeInfo> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(@NonNull BaseViewHolder baseViewHolder, EvaluateWadeInfo evaluateWadeInfo) {

            if (evaluateWadeInfo != null) {

                //是否被勾中
                boolean isChecked = evaluateWadeInfo.isChecked();
                //填充名称：
                String typeName = evaluateWadeInfo.getDictLabel();
                //车牌号码
                String carNo = evaluateWadeInfo.getCarNumber();

                //填充名称
                TextView textViewStationNameType = (TextView) baseViewHolder.getView(R.id.textviewstationinfonametwo);
                //勾选图标
                ImageView checkBoxInStationType = (ImageView) baseViewHolder.getView(R.id.checkboxinstationnametwo);

                //手机号-如果 餐券和客户关怀-卡券 是要展示手机号的
                TextView textView_user_phone_number = (TextView) baseViewHolder.getView(R.id.textview_user_phone_numbertwo);

                //车牌号信息
                TextView textViewCardNo = (TextView) baseViewHolder.getView(R.id.textviewcardnotwo);


                if (extra_jumpfrom_where == ExtraValue.MODIFY_EVALUATE_RICE_TICKET_PERSON_SELECT//
                        || extra_jumpfrom_where == ExtraValue.MODIFY_EVALUATE_CARMAIN_PERSON_SELECT) { //

                    if (textView_user_phone_number != null) {
                        textView_user_phone_number.setVisibility(View.VISIBLE);
                        textView_user_phone_number.setText(evaluateWadeInfo.getUserPhoneNumber());
                    }

                } else {
                    if (textView_user_phone_number != null) {
                        textView_user_phone_number.setVisibility(View.GONE);
                        textView_user_phone_number.setText("");
                    }
                }

                //填充名称
                CommUtils.setText(textViewStationNameType, typeName);

                //填充车牌号信息
                CommUtils.setText(textViewCardNo, carNo);

                if (isChecked) {//表示选中
                    //字体设置为选中
//                    textViewStationNameType.setTextColor(ColorUtils.getColor(R.color.orangeygxsk));
                    textViewStationNameType.setTextColor(ColorUtils.getColor(R.color.redcomm));
                    checkBoxInStationType.setVisibility(View.VISIBLE);

                } else {

                    textViewStationNameType.setTextColor(ColorUtils.getColor(R.color.dark));
                    checkBoxInStationType.setVisibility(INVISIBLE);

                }

            }

        }
    }

}
